#!/bin/bash

set -e

if [ $# -lt 4 ]; then
    echo "Usage: $0 $# <dummy_port> <altID> <usbID> <binfile>" >&2
    exit 1
fi
dummy_port=$1; altID=$2; usbID=$3; binfile=$4;dummy_port_fullpath="/dev/$1"

#if we can find the Serial device try resetting it and then sleeping for 1 sec while the board reboots

if [ -e $dummy_port_fullpath ]; then
	echo "resetting " $dummy_port_fullpath
	stty -f $dummy_port_fullpath 1200
	sleep 1
#	stty -f $dummy_port_fullpath 1200
#	sleep 1
fi


DFU_UTIL=/usr/local/bin/dfu-util
if [ ! -x ${DFU_UTIL} ]; then
    DFU_UTIL=/opt/local/bin/dfu-util
fi

if [ ! -x ${DFU_UTIL} ]; then
    echo "$0: error: cannot find ${DFU_UTIL}" >&2
    exit 2
fi

${DFU_UTIL} -d ${usbID} -a ${altID} -D ${binfile} -R
